package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.constant.DictEnum;
import com.workcell.workdata.xthings.dto.DictVo;
import com.workcell.workdata.xthings.entity.Dict;
import com.workcell.workdata.xthings.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(value = "DictController", tags = "字典")
@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
public class DictController {
    private final DictService dictService;

    @ApiOperation("根据字典类型编码获取字典项")
    @GetMapping("/getByDictTypeCode")
    public R<List<Dict>> getByDictTypeCode(String dictTypeCode) {
        Assert.notEmpty(dictTypeCode, "字典类型编码不能为空");
        return R.ok(dictService.getByDictTypeCode(dictTypeCode));
    }

    @ApiOperation("保存字典项")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody DictVo dictVo) {
        return R.ok(dictService.save(dictVo));
    }

    @ApiOperation("串口工作模式字典项")
    @GetMapping("/serialWorkingMode")
    public R<List<Dict>> serialWorkingMode() {
        return R.ok(dictService.getByDictTypeCode(DictEnum.SERIAL_WORKING_MODE.getDictTypeCode()));
    }
}
